package com.baofu.paycore.web.controller;

import com.baofu.paycore.service.RechargeService;
import com.baofu.paycore.service.facade.model.FundBaseDTO;
import com.baofu.paycore.service.facade.model.WebPayResDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeDTO;
import com.system.commons.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.UUID;

/**
 * 充值控制器
 *
 * <p>
 * 1.
 * </p>
 * User: xin deng  Date: 16/5/18 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;

//    @RequestMapping("/pay/webRecharge")
    public String webRecharge(RechargeDTO rechargeDTO, Model model){
        try {
            rechargeDTO = new RechargeDTO();
            rechargeDTO.setRequestSystem("TRADE");
            rechargeDTO.setRequestChannel("WEB");
            rechargeDTO.setRequestDate(new Date());
            rechargeDTO.setRequestBizNo(String.valueOf(System.currentTimeMillis()) + "abcdefg");
            rechargeDTO.setRequestOrderNo(String.valueOf(System.currentTimeMillis()) + "abdddcdefg");
            rechargeDTO.setCcy("CNY");
            rechargeDTO.setAccountNo("97100000000423156");
            rechargeDTO.setBizType("充值");
            rechargeDTO.setServiceCode("RECHARGE");
            rechargeDTO.setSubBizType("快捷支付");
            rechargeDTO.setAmt(3l);
            rechargeDTO.setEventNo("30000001");
            rechargeDTO.setContractNo("3177000000000044");

            FundBaseDTO rechargeFundDTO = new FundBaseDTO();
            rechargeFundDTO.setPayToolType("BANK_CARD");
            rechargeFundDTO.setPayToolSubType("Account");
            rechargeFundDTO.setCcy("CNY");
            rechargeFundDTO.setAmt(3l);
            rechargeFundDTO.setBankCode("ICBC_D");
            rechargeFundDTO.setCardType("D");
            rechargeFundDTO.setAgreeRelId("1");
            rechargeFundDTO.setAgreementNo("1231231312");


            log.info("web充值:参数:{}",rechargeDTO);
            rechargeDTO.setFundBaseDTO(rechargeFundDTO);

            Result<WebPayResDTO> result = rechargeService.webRecharge(rechargeDTO, UUID.randomUUID().toString());
            if (!result.isSuccess()){
                return null;
            }
            model.addAttribute("url", result.getResult().getOutPayUrl());
            model.addAttribute("params", result.getResult().getParams());
        } catch (Exception e) {
            log.error("web 充值异常:{}",e);
        }
        return "redirect";
    }
}
